<template>
  <div>
    <el-form ref="formRef" :model="form" label-width="80px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="form.mobile" placeholder="请输入手机号!" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-col :span="11">
          <el-date-picker v-model="form.timeOfEntry" format="yyyy年MM月dd日" value-format="yyyy-MM-dd" type="date" placeholder="请选择入职时间" style="width: 100%;" />
        </el-col>
      </el-form-item>
      <el-form-item label="员工头像">
        <el-col :span="8">
          <!-- <el-input v-model="form.active" type="textarea" /> -->
          <!-- <img :src="form.staffPhoto" alt="" width="100px"> -->
          <UploadImg :key="form.id" :src="form.staffPhoto" @upload-success="uploadSuccess" />
        </el-col>
      </el-form-item>
      <el-button type="primary" class="btn" @click="onSubmit">保存更新</el-button>
      <el-button type="worning" class="btn">返回</el-button>

    </el-form>
  </div>
</template>
<script>
import { getUserInfoRequest, updateUserInfoRequest } from '@/api/employees'

export default {
  name: '',
  data() {
    return {
      form: {
        mobile: '',
        timeOfEntry: '',
        active: ''
      }
    }
  },
  created() {
    this.getUserInfo()
  },
  methods: {
    onSubmit() {
      // console.log(this.form)
      this.$refs.formRef.validate(v => {
        console.log(v)
        if (v) {
          this.updateUserInfo()
        }
      })
    },
    async getUserInfo() {
      // console.log(this.$route.query.id)
      try {
        const res = await getUserInfoRequest(this.$route.query.id || '')
        console.log('getUserInfoRequest: ', res)
        if (res.success) {
        //   this.form.username = res.data.username
        //   this.form.password = res.data.password
          this.form = res.data
        }
      } catch (err) {
        console.log('getUserInfoRequest: ', err)
      }
    },
    // 代码抽离的作用:因为请求里面有try catch 有业务成功业务失败,代码比较多,提交按钮事件onSubmit里面,还要写兜底校验,不方便写业务代码
    // 个人信息更新
    async updateUserInfo() {
      try {
        const res = await updateUserInfoRequest(this.form)
        console.log('updateUserInfoRequest: ', res)
        if (res.success) {
          this.$message.success(res.message)
          this.$router.back()
        } else {
          this.$message.error(res.message)
        }
      } catch (err) {
        console.log('updateUserInfoRequest: ', err)
      }
    },
    uploadSuccess(img) {
      console.log('子组件传的值', img)
      this.form.staffPhoto = img
      console.log(this.form)
    }
  }
}
</script>
